package com.zzhua.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

@Component    // 这个bean通过组合的方式给到了SchedulerFactoryBean，相当于是它的一个组件
public class MyJobFactory extends AdaptableJobFactory {
      
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;
  
    @Override  
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object job = super.createJobInstance(bundle);
        										
        capableBeanFactory.autowireBean(job); // 读取job的@Autowired注解，完成job对象的自动注入装配
        return job;
    }

}